package com.dhcc.bpm.modules.desk.controller;

import com.dhcc.bpm.common.redisMessage.RedisService;
import com.dhcc.bpm.common.utils.DateUtils;
import com.dhcc.bpm.modules.desk.vo.ApiSysVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

/**
 * @ClassName RedisController
 * @Description TODO
 * @Author wangjinghan
 * @Date 2020/11/20 9:38
 * @Version 1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-redis")
@Api(value = "应用/渠道信息", tags = "reids接口")
public class RedisController {
    @Autowired
    RedisService redisService;


    /**
     * @Author wangjinghan
     * @Description api生产系统
     * @date: 2020/11/19 16:23
     */
    @GetMapping("/apiSysListR")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "api生产系统", notes = "api生产系统")
    @ApiLog("获取api生产系统")
    public R<List<List<ApiSysVO>>> apiSysListR() {
        String nowDate = DateUtils.dateTime();
        //从redis中获取所有生产系统名称
        List<String> lists = redisService.hvals("apiCode:apiCode" + nowDate);
        List<String> list = new ArrayList<>(new TreeSet<>(lists));//去重
        // 整理给前台返回格式
        List<List<ApiSysVO>> totalList = new ArrayList<>();

        for (String sysName : list) {
            List<ApiSysVO> resList = new ArrayList<>();
            for (int i = 0; i < 24; i++) {
                ApiSysVO apiSysVO = new ApiSysVO();
                apiSysVO.setName(sysName);
                //找对应的访问数量
                if (i < 10) {
                    apiSysVO.setTime("0" + i + ":00");
                    Object val = redisService.hashGet("apiCode:sys" + nowDate, sysName + nowDate + "0" + i);
                    if (val != null) {
                        apiSysVO.setCount(Long.parseLong(val.toString()));
                    }
                } else {
                    apiSysVO.setTime(i + ":00");
                    Object val = redisService.hashGet("apiCode:sys" + nowDate, sysName + nowDate + i);
                    if (val != null) {
                        apiSysVO.setCount(Long.parseLong(val.toString()));
                    }
                }
                resList.add(apiSysVO);
            }
            totalList.add(resList);
        }
        return R.data(totalList);
    }
}
